package resp

import (
	"github.com/gin-gonic/gin"
	"go-malphite/app/common/state"
	"net/http"
)

type Response struct {
	Code state.Exception `json:"code"`
	Data interface{}     `json:"data"`
	Msg  string          `json:"msg"`
}

func result(c *gin.Context, code state.Exception, data interface{}) {
	c.JSON(http.StatusOK, Response{
		Code: code,
		Data: data,
		Msg:  code.String(),
	})
}

func resultWithMsg(c *gin.Context, code state.Exception, data interface{}, msg string) {
	c.JSON(http.StatusOK, Response{
		Code: code,
		Data: data,
		Msg:  msg,
	})
}

func OK(c *gin.Context, data interface{}) {
	result(c, state.Success, data)
}

func OkWithMsg(c *gin.Context, msg string) {
	resultWithMsg(c, state.Success, nil, msg)
}

func OkWithData(c *gin.Context, data interface{}, msg string) {
	resultWithMsg(c, state.Success, data, msg)
}

func Fail(c *gin.Context) {
	result(c, state.Fail, nil)
}

func FailWithMsg(c *gin.Context, msg string) {
	resultWithMsg(c, state.Fail, nil, msg)
}

func FailWithCode(c *gin.Context, code state.Exception) {
	result(c, code, nil)
}

func FailWithCodeAndMsg(c *gin.Context, code state.Exception, msg string) {
	resultWithMsg(c, code, nil, msg)
}
